package com.icode.web.controller.house;

import com.icode.core.dto.house.ExchangePoolDTO;
import com.icode.core.model.house.ExchangePool;
import com.icode.core.model.house.ProductType;
import com.icode.core.model.user.User;
import com.icode.core.shared.ExchangeAlipayHandle;
import com.icode.core.shared.SecurityUtil;
import com.icode.service.house.ExchangeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.io.PrintWriter;
import java.math.BigDecimal;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: xianfei
 * Date: 15-8-24
 * Time: 下午3:26
 * To change this template use File | Settings | File Templates.
 */
@Controller
@RequestMapping(value = "/exchange")
public class ExchangeController {

    @Autowired
    private ExchangeService exchangeService;

    @RequestMapping(value = "choose_exchange/{myHouseGuid}", method = RequestMethod.GET)
    public String showAllowExchange(@PathVariable String myHouseGuid, Model model) {
        //判断加入交换池，查出所有可交换的房
       // exchangeService.chooseExchange(myHouseGuid);
        List<ExchangePoolDTO> exchangePoolDTOs = exchangeService.findExchangePoolByMyHouse(myHouseGuid);
        List<ExchangePoolDTO> userExchangePoolDTOs = exchangeService.findExchangePoolByUserInfo(myHouseGuid);
        model.addAttribute("exchangePoolDTOs", exchangePoolDTOs);
        model.addAttribute("userExchangePoolDTOs", userExchangePoolDTOs);
        return "front/exchange/exchange";
    }

//    @RequestMapping(value = "add/{myHouseGuid}", method = RequestMethod.GET)
//    @ResponseBody
//    public Object addToExchangePool(@PathVariable String myHouseGuid) throws Exception {
//        return exchangeService.addToExchangePool(myHouseGuid);
//    }

    @RequestMapping(value = "add/{myHouseGuid}", method = RequestMethod.GET)
    public String addToExchangePool(@PathVariable String myHouseGuid) throws Exception {
            exchangeService.addToExchangePool(myHouseGuid);
           return "redirect:/exchange/choose_exchange/" + myHouseGuid;
    }

    @RequestMapping(value = "list/{type}/{userInfoGuid}", method = RequestMethod.GET)
    @ResponseBody
    public Object findAllExchangePool(@PathVariable String type, @PathVariable String userInfoGuid) {
        List<ExchangePoolDTO> exchangePoolDTOs = exchangeService.findAllExchangePool(type, userInfoGuid);
        return exchangePoolDTOs;
    }

    @RequestMapping(value = "do_exchange/{userExchangePoolGuid}/{exchangePoolGuid}", method = RequestMethod.GET)
    public void doExchange(@PathVariable String userExchangePoolGuid, @PathVariable String exchangePoolGuid, PrintWriter out) {
        BigDecimal differ = exchangeService.toConfirmExchange(userExchangePoolGuid, exchangePoolGuid);
        ExchangePool exchangePool = exchangeService.findSingleExchangePool(exchangePoolGuid);
        ProductType type = exchangePool.myHouse().houseInfo().buildingType().houseBase().productType();
        BigDecimal fee = new BigDecimal(0);
        if (type == ProductType.HOUSEBASE) {
            fee = differ.add(new BigDecimal(200));
            ExchangeAlipayHandle.alipay(userExchangePoolGuid, exchangePool, String.valueOf(200), out);
        } else if (type == ProductType.APARTMENT) {
            fee = differ.add(new BigDecimal(100));
            ExchangeAlipayHandle.alipay(userExchangePoolGuid, exchangePool, String.valueOf(100), out);
        }
//        ExchangeAlipayHandle.alipay(userExchangePoolGuid, exchangePool, String.valueOf(0.1), out);
    }

    @RequestMapping(value = "return_url/{userExchangePoolGuid}", method = RequestMethod.GET)
    public String exchangeReturnUrl(@PathVariable String userExchangePoolGuid, HttpServletRequest request, PrintWriter out) {
        User user = SecurityUtil.currentUser();
        ExchangeAlipayHandle.return_url(userExchangePoolGuid, request, exchangeService, user, out);
        return "redirect:/my_house/show";
    }

    @RequestMapping(value = "notify_url/{userExchangePoolGuid}", method = RequestMethod.POST)
    public void exchangeNotifyUrl(@PathVariable String userExchangePoolGuid, HttpServletRequest request, PrintWriter out) {
        User user = SecurityUtil.currentUser();
        ExchangeAlipayHandle.notify_url(userExchangePoolGuid, request, exchangeService, user, out);
    }

    @RequestMapping(value = "confirm_exchange/{userExchangePoolGuid}/{exchangePoolGuid}", method = RequestMethod.GET)
    public String confirmExchange(@PathVariable String userExchangePoolGuid, @PathVariable String exchangePoolGuid, Model model) {
        BigDecimal differ = exchangeService.toConfirmExchange(userExchangePoolGuid, exchangePoolGuid);
        String fee = exchangeService.getServiceFee(exchangePoolGuid);
        ExchangePoolDTO userExchangePoolDTO = exchangeService.findExchangePoolByGuid(userExchangePoolGuid);
        ExchangePoolDTO exchangePoolDTO = exchangeService.findExchangePoolByGuid(exchangePoolGuid);
        model.addAttribute("differ", differ.toString());
        model.addAttribute("fee", fee);
        model.addAttribute("userExchangePoolDTO", userExchangePoolDTO);
        model.addAttribute("exchangePoolDTO", exchangePoolDTO);
        return "front/exchange/confirmExchange";
    }
}
